package sv.view;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import sv.main.*;
//import sv.net.*;
import sv.net2.NetConnection;

public class View extends JFrame {
	
	private JPanel ownSide;
	private JPanel oppSide;
	private JPanel ownBoard;
	private JPanel oppBoard;
	private Tile[][] ownField;
	private Tile[][] oppField;
	private JTextArea log;
	private JScrollPane sp;
	private boolean settingShips;
	//private BitmapComponent ship5;
	private MouseFieldAdapter mouse;
	private MouseBattleAdapter battleMouse;
	private Game game;
	private MyNetwork connection;
	
	public View(Game ogame, MyNetwork con, JTextArea ta){
		super("Schiffe versenken");
		addWindowListener(new WindowClosingAdapter(true));
		setLocation(60, 100);
		setSize(810, 410);
		setResizable(false);
		game = ogame;
		connection = con;
		
		ownSide = new JPanel();
		ownSide.setBorder(BorderFactory.createLineBorder(Color.orange));
		ownSide.setLayout(null);
		oppSide = new JPanel();
		oppSide.setBorder(BorderFactory.createLineBorder(Color.orange));
		oppSide.setLayout(null);
		
		ownBoard = new JPanel();
		ownBoard.setSize(200,200);
		ownBoard.setLocation(100,100);
		ownBoard.setLayout(new GridLayout(10,10));
		oppBoard = new JPanel();
		oppBoard.setSize(200,200);
		oppBoard.setLocation(100,100);
		oppBoard.setLayout(new GridLayout(10,10));
		
		ownField = new Tile[10][10];
		oppField = new Tile[10][10];
		
		log = ta;
		log.setSize(390,80);
		log.setLocation(0,0);
		log.setBorder(BorderFactory.createEtchedBorder());
		log.setEditable(false);
		
		sp = new JScrollPane(log);
		sp.setLocation(0,0);
		sp.setSize(390,80);
		sp.setBorder(BorderFactory.createEtchedBorder());
		
		mouse = new MouseFieldAdapter(game, log, this, connection);
		battleMouse = new MouseBattleAdapter(connection, game, log);
				
		Container contentPane = getContentPane();
		contentPane.setLayout(new GridLayout(1,2));
		add(ownSide);
		add(oppSide);
		ownSide.add(ownBoard);
		ownSide.add(sp);
		oppSide.add(oppBoard);
				
		for (byte i=0; i<10; i++){
			for (byte j=0; j<10; j++){
				ownField[i][j] = new Tile(i , j);
				ownField[i][j].setBorder(BorderFactory.createLineBorder(Color.gray, 1));
				ownField[i][j].setLayout(null);
				ownField[i][j].setBackground(Color.blue);
				ownField[i][j].addMouseListener(mouse);
				ownBoard.add(ownField[i][j]);
				oppField[i][j] = new Tile(i , j);
				oppField[i][j].setBorder(BorderFactory.createLineBorder(Color.gray, 1));
				oppField[i][j].setBackground(Color.blue);
				oppBoard.add(oppField[i][j]);
			}
		}
		connection.setOwnTile(ownField);
		connection.setOppTile(oppField);
		
		settingShips = true;
		setVisible(true);
	}

	public void enableBattleAdapter(){
		for (byte i=0; i<10; i++){
			for (byte j=0; j<10; j++){
				oppField[i][j].addMouseListener(battleMouse);
				ownField[i][j].removeMouseListener(mouse);
			}
		}
	}
	
}

